package com.sinosoft.web.easyui.api;


import cn.com.sinux.spring.commons.PropertyCollection;
import com.sinosoft.hydra.um.condition.SearchResourceCondition;
import com.sinosoft.hydra.um.domain.permission.SysResource;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.SysResourceService;
import cn.com.sinux.spring.utils.BeanUtils;
import cn.com.sinux.spring.utils.PageUtils;
import cn.com.sinux.spring.json.JsonFilter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author liangminga
 * @since 0.5-alpha
 * 2014年12月17日
 * 描述：进行资源的增删查改操作。验证增加资源时资源名称重复，编辑资源时资源名称重复的功能。以及跳转到用户资源页面。
 */
@Controller
@RequestMapping("api/resource")
public class SysResourceApi {
    /**
     * 系统资源服务类
     */
    @Autowired
    private SysResourceService resourceService;

    /**
     * ajax添加资源
     *
     * @param resource 资源对象
     * @return String {flag:200,msg:成功},{flag:500,msg:失败}
     */
    @RequestMapping(value = "addResourceForJson", method = RequestMethod.POST)
    @RequiresPermissions(value = "resource:data:add")
    @ResponseBody
    public Object addResourceForJson(SysResource resource) {
        SysResource result = resourceService.addResource(resource);
        LoggerHelper.logInfo("添加了权限项‘{}’", resource.getResourceName());
        return JsonFilter.doPropertyFilter(result, new PropertyCollection()
                .addProperties(SysResource.class, "id", "resourceName"));
    }

    /**
     * ajax删除资源
     *
     * @param searchResourceCondition 资源主键集
     * @return String {flag:200,msg:成功},{flag:500,msg:失败}
     */
    @RequestMapping(value = "deleteResourceForJsonIds", method = RequestMethod.POST)
    @RequiresPermissions(value = "resource:data:delete")
    @ResponseBody
    public Object deleteResourceForJsonIds(SearchResourceCondition searchResourceCondition) {
        List<SysResource> resources = resourceService.deleteSysResource(searchResourceCondition.getIds());
        LoggerHelper.logInfo("删除了权限‘{}’", new LoggerHelper.PropertyReader<SysResource>(
                resources,
                "resourceName"
        ));
        return JsonFilter.doPropertyFilter(resources, new PropertyCollection()
                .addProperties(SysResource.class, "id", "resourceName"));
    }

    /**
     * ajax修改资源
     *
     * @param resource 资源
     * @return String {flag:200,msg:成功},{flag:500,msg:失败}
     */
    @RequestMapping(value = "updateResourceForJson", method = RequestMethod.POST)
    @RequiresPermissions(value = "resource:data:update")
    @ResponseBody
    public Object updateResourceForJson(SysResource resource) {
        SysResource db = resourceService.findOne(resource.getId());
        SysResource source = new SysResource();
        BeanUtils.copyProperties(db, source);
        SysResource sysResource = resourceService.updateResource(resource);
        LoggerHelper.logInfo("对权限‘{}’的信息进行了修改“{}”", db.getResourceName(), new LoggerHelper.PropertyMerge(source, sysResource)
                        .addPropertyDes("resourceName", "资源名称")
                        .addPropertyDes("resourceUrl", "资源路径")
                        .addPropertyDes("parentId", "父级资源编号")
                        .addPropertyDes("orderNo", "资源序号")
                        .addPropertyDes("available", "是否使用")
                        .addPropertyDes("icon", "资源图标")
        );
        return JsonFilter.doPropertyFilter(sysResource, new PropertyCollection()
                .addProperties(SysResource.class, "id", "resourceName"));
    }

    /**
     * ajax分页查询
     *
     * @param pageNumber 页数
     * @param pageSize   页大小
     * @param resource   资源对象
     * @return String 资源集字符串
     */
    @RequestMapping("queryResourceByPageForJson")
    @ResponseBody
    public Object queryResourceByPageForJson(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize,
            SearchResourceCondition resource
    ) {
        resource.setLikeWords(true);
        Page<SysResource> page = resourceService.findAllByPage(PageUtils.buildPageRequest(pageNumber, pageSize, "id", "desc"), resource);
        return JsonFilter.doPropertyFilter(page, new PropertyCollection().addProperties(SysResource.class, "id", "permission", "resourceName", "resourceType",
                "resourceUrl", "available", "sysResource"));
    }

    /**
     * 查询资源角色树（角色继承）
     *
     * @param id 角色id
     * @return String 节点集字符串
     */
    @RequestMapping("roleResources")
    @ResponseBody
    public Object roleResource(@RequestParam(required = false, defaultValue = "") String id) {
        return resourceService.roleResources(id);
    }

    /**
     * 资源搜索页面
     *
     * @param model 模型
     * @return String 页面名称
     */
    @RequestMapping("search")
    @RequiresPermissions(value = "resource:data:list")
    public String search(Model model) {
        return "hydra_easyui/resource/search";
    }


    /**
     * 新增资源页面
     *
     * @return String 页面名称
     */
    @RequestMapping("toAdd")
    @RequiresPermissions(value = "resource:data:add")
    public String toAdd() {
        return "hydra_easyui/resource/addResource";
    }

    /**
     * 资源搜索页面
     *
     * @param model       模型
     * @param sysResource 资源对象
     * @return String 页面名称
     */
    @RequestMapping("toUpdateResource")
    @RequiresPermissions(value = "resource:data:update")
    public String toUpdate(Model model, SysResource sysResource) {
        SysResource sr = resourceService.findOne(sysResource.getId());
        model.addAttribute("resource", sr);
        return "hydra_easyui/resource/updateResource";
    }

    /**
     * 跳转到资源列表页面。
     *
     * @return String 页面名称
     */
    @RequestMapping(value = "resourcePage")
    @RequiresPermissions(value = "resource:data:list")
    public String userPage() {
//        LoggerHelper.logInfo("访问了资源列表页");
        return "hydra_easyui/resource/index";
    }
}
